package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController("adminDishController")
@RequestMapping(value = "/admin/dish")
@Api(tags = "菜品管理相关接口")
public class DishController {
    @Autowired
    private DishService dshService;
    @Autowired
    private RedisTemplate  redisTemplate;

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO  dishPageQueryDTO){
        log.info("分页查询:{}",dishPageQueryDTO);
        PageResult pageResult = dshService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("修改菜品状态")
    @PostMapping("/status/{status}")
    public Result<String> chanageStatus(@PathVariable Integer status, long id){
        log.info("修改菜品状态:{}",status);
        dshService.changestatus(status,id);
        //将所有的菜品的缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("{id}")
    public Result<DishVO> getDishById(@PathVariable Integer id){
        log.info("根据id查询菜品:{}",id);
        DishVO  dishVO = dshService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCateId(Integer categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        List<Dish>  dishes = dshService.getDishByCateId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @DeleteMapping
    public Result<String> CountDelete(Integer[] ids){
        log.info("批量删除:{}",ids);
        //List<Integer> idList = Arrays.asList(ids);
        dshService.deleteByIds(Arrays.asList(ids));
        //将所有的菜品的缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 新增菜品
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO  dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dshService.addDish(dishDTO);
        String key ="dish_"+dishDTO.getCategoryId();
        //删除redis中的缓存
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> editDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dshService.editDish(dishDTO);
        //将所有的菜品的缓存都删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

}
